<template>
  <div class="mergeInfo">
    <basic-container>
      <el-dialog
        title="数据来源替换"
        :visible.sync="dialogVisible"
        @close="closed"
        width="950px">
        <div style="line-height: 40px;background-color: #fcf7f7;padding-left: 15px">
          <span>线路：</span><span>{{getLineName(formData.lineCode)}}</span>
          <span style="margin-left: 20px">站点：</span><span>{{getStationName(formData.lineCode,formData.stationCode)}}</span>
          <span style="margin-left: 20px"> TVM编号：</span><span>{{formData.tvmCode}}</span>
          <!-- <span style="margin-left: 20px"> 钱箱编号：</span><span>{{formData.cashBoxCode}}</span> -->
          <span style="margin-left: 20px"> 钱箱类型：</span><span>{{formData.cashBoxTypeName}}</span>
          <span style="margin-left: 20px"> 补币金额：</span><span>{{fenExYuangettoFixed(formData,'supplementMoney')}}</span>
          <span style="margin-left: 20px"> 卸载金额：</span><span>{{fenExYuangettoFixed(formData,'actualMoney')}}</span>
<!--          <span style="margin-left: 20px"> 手工清出金额：</span><span>{{fenExYuangettoFixed(formData,'handOutMoney')}}</span>-->
          <span style="margin-left: 20px"> 手工清出金额：</span><span>{{fenExYuangettoFixed2(handOutHeJiAmount)}}</span>

<!--          <span style="margin-left: 20px"> 清出合计金额：</span><span>{{heji((formData.actualMoney-formData.supplementMoney+formData.handOutMoney))}}</span>-->
          <span style="margin-left: 20px"> 清出合计金额：</span><span>{{fenExYuangettoFixed2(clearOutHeJiAmount)}}</span>

        </div>
      <avue-crud
        ref="mergeInfo"
        :option="option"
        :table-loading="listLoading"
        :data="list"
        :page.sync="page">
        <template slot-scope="scope" slot="menu">
          <el-button type="text" v-if="scope.row.auditStatus==0 && scope.row.dataSource==2 " size="small" plain @click.stop="syncSystemAmounts(scope.row)">刷新</el-button>
          <el-button type="text" size="small" v-if="scope.row.auditStatus==0" plain @click.stop="replaceSystemAmounts(scope.row)">替换</el-button>
        </template>
      </avue-crud>
        <div style="width: 100%;text-align: center">
          <el-button type="primary" size="mini"  @click="closed">返回</el-button>
        </div>
      </el-dialog>
    </basic-container>

  </div>

</template>

<script>
  import {getForm, postForm} from '@/api/gen/gen'
  import {mapGetters} from 'vuex';
  import {
    setSearchFiledsEmpty, getStationName, getLineName, fenExYuangettoFixed, getSCOrLCOrACC, fenExYuangettoFixed2
  } from "../../../../api/subwayRat/commonCheck";
  import {replaceSystemAmounts,heji,syncCashBoxBatchAuditSystemAmounts,getClearOutAmount
  } from '../../../../api/subwayRat/TVMRevenueVarianceAudit/cashBoxBatchAudit/index';
  import {getListCionMOneyById} from "@/api/subwayRat/TVMRevenueVarianceAudit/cashBoxBatchAudit";
  import { dateFormat } from "@/util/date";
  export default {
    name: 'huLianWangShouPiaojiDataInput',
    components:{
    },
    props: {
      home: Object
    },
    data() {
      return {
        dialogVisible:false,
        option: {},
        listLoading: true,
        lineCodes:[],
        stationCodes:[],
        selectKeys:[],
        formData:{},
        selectKeyVals:[],
        searchForm:{},
        clearOutHeJiAmount:0,
        handOutHeJiAmount:0,
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条,
          isAsc: false// 是否倒序
        },
        list : []
      }
    },
    computed: {
      ...mapGetters(['permissions']),
    },
    watch: {
    },
    created() {
      this.option={
        refreshBtn:false,
        columnBtn:false,
        emptyBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        border: true,
        index: true,
        indexLabel: '序号',
        page: true,
        align: 'center',
        menuWidth: 150,
        menuAlign: 'center',
        selection: false,
        reserveSelection: true,
        column: [{
          fixed: true,
          label: '主键',
          prop: 'id',
          span: 24,
          hide: true
        },{label: "数据来源", prop: "dataSource",formatter :function(row, value , label, column){
            return getSCOrLCOrACC(value);
          }},
          {label: "系统金额", prop: "sellAmount",formatter:function (row, value , label, column){

              return fenExYuangettoFixed2(value);
            }},
          {label: "差异金额", prop: "investAmount",formatter:function (row, value , label, column){

              return fenExYuangettoFixed2(value);
            }},
          {label: "SC数据卸载金额",hide:true, prop: "scUnloadAmount",formatter:function (row, value , label, column){

              return fenExYuangettoFixed2(value);
            }}
        ]
      };
    },
    methods: {
      getStationName,getLineName,
      fenExYuangettoFixed,
      fenExYuangettoFixed2,
      setSearchFiledsEmpty,
      heji,
      syncSystemAmounts(row){
        let th=this;
        syncCashBoxBatchAuditSystemAmounts({row:JSON.stringify(this.formData),freshType:row.dataSource}).then(res=>{
          if(res.data.code==0){
            th.$message.success("操作成功！");
            th.$parent.getList();
            this.$nextTick(()=>{
              this.getReplaceLists(this.home.replaceRow);
            })
            //th.closed();
          }else{
            th.$message.error("操作失败！");
          }
        }).catch(e=>{
          th.$message.error("操作失败！");
        })
      },
      getReplaceLists(row) {
        this.list = [];
        let rowData = {};
        getListCionMOneyById(row).then((response)=>{
          rowData = response.data;
          rowData.incomeSettleBeginTime = dateFormat(new Date(rowData.incomeSettleBeginTime)).substr(0, 10);
          rowData.incomeSettleEndTime = dateFormat(new Date(rowData.incomeSettleEndTime)).substr(0, 10);
          this.formData = rowData;
          this.$nextTick(()=>{
            this.clearOutHeJiAmountFc();
            this.handOutHeJiAmountFc();
          })
          this.listLoading = true;
          let CY = this.clearOutHeJiAmount;
          let JZDJ = {
            "id":rowData.id,
            "auditStatus":rowData.auditStatus,
            "dataSource":1,
            "scUnloadAmount":0,
            "sellAmount":rowData.billOrderMoney,
            "investAmount":CY-rowData.billOrderMoney
          };
          let SC = {
            "id":rowData.id,
            "auditStatus":rowData.auditStatus,
            "dataSource":2,
            "scUnloadAmount":rowData.scUnloadAmount,
            "sellAmount":rowData.scUnloadAmount,
            "investAmount":CY-rowData.scUnloadAmount
          };
          let FQX = {
            "id":rowData.id,
            "auditStatus":rowData.auditStatus,
            "dataSource":6,
            "scUnloadAmount":0,
            "sellAmount":row.replyUnloadDisplayMoney,
            "investAmount":CY - (row.replyUnloadDisplayMoney),
          };
          this.list.push(JZDJ);
          this.list.push(SC);
          this.list.push(FQX);
          this.listLoading = false;
        }); 
      },
      async getLists(row) {
        this.list = [];
        this.formData=row;
        await this.clearOutHeJiAmountFc();
        await this.handOutHeJiAmountFc();
        this.listLoading = true;

        //let CY = row.actualMoney-row.supplementMoney+row.handOutMoney;
        // let CY = row.actualMoney+row.handOutMoney;
        let CY = this.clearOutHeJiAmount;
        let JZDJ = {
          "id":row.id,
          "auditStatus":row.auditStatus,
          "dataSource":1,
          "scUnloadAmount":0,
          "sellAmount":row.billOrderMoney,
          "investAmount":CY-row.billOrderMoney
        };
        let SC = {
          "id":row.id,
          "auditStatus":row.auditStatus,
          "dataSource":2,
          "scUnloadAmount":row.scUnloadAmount,
          "sellAmount":row.scUnloadAmount,
          "investAmount":CY-row.scUnloadAmount
        };
        let FQX = {
          "id":row.id,
          "auditStatus":row.auditStatus,
          "dataSource":6,
          "scUnloadAmount":0,
          "sellAmount":row.replyUnloadDisplayMoney,
          "investAmount":CY - (row.replyUnloadDisplayMoney),
        };
        this.list.push(JZDJ);
        this.list.push(SC);
        this.list.push(FQX);
        this.listLoading = false;
      },
      replaceSystemAmounts(row){ //接口来源替换
        let th=this;
        this.formData.dataSource=row.dataSource;
        replaceSystemAmounts(this.formData).then(res=>{
          if(res.data.code==0){
            th.$message.success("操作成功！");
            th.dialogVisible=false;
            th.$parent.getList();
            th.$parent.isShowMergeInfo=false;
          }else{
            th.$message.error("操作失败！");
          }
        }).catch(e=>{
          th.$message.error("操作失败！");
        })
      },
      async clearOutHeJiAmountFc(){//清出合计金额
  //标题“合计清点收入金额”改为“清出合计金额”；当为硬币钱箱时，金额值为两种硬币钱箱卸载金额合计+手工清出硬币；
        // 当为纸币回收箱时，金额值为纸币回收箱卸载金额+手工清出纸币；当为纸币找零箱时，金额值为纸币找零箱卸载金额。
        if(this.formData.cashBoxType==6){ //硬币钱箱
          let res=await getClearOutAmount(this.formData);
          this.clearOutHeJiAmount = this.formData.actualMoney+res.data.data[0].clear_out_amount;
        }
        if(this.formData.cashBoxType==1){ //硬币回收
          let res=await getClearOutAmount(this.formData);
          this.clearOutHeJiAmount = this.formData.actualMoney+res.data.data[0].clear_out_amount;
        }
        if(this.formData.cashBoxType==2){ //硬币找零
          this.clearOutHeJiAmount = this.formData.actualMoney;
        }
        if(this.formData.cashBoxType==3){ //纸币回收
          let res=await getClearOutAmount(this.formData);
          this.clearOutHeJiAmount =  this.formData.actualMoney+res.data.data[0].clear_out_paper_amount;
        }
        if(this.formData.cashBoxType==4){ //纸币找零
          this.clearOutHeJiAmount =  this.formData.actualMoney;
        }
        if(this.formData.cashBoxType==5){ //废钞箱
          this.clearOutHeJiAmount = this.formData.actualMoney;
        }
      },
      async handOutHeJiAmountFc(){//手工清出金额
        //当为纸币回收箱时，取手工清出表的清出纸币字段值；当为纸币找零箱时为0；当为硬币钱箱时，取手工清出表的清出硬币字段值。
        if(this.formData.cashBoxType==6){ //硬币钱箱
          let res=await getClearOutAmount(this.formData);
          this.handOutHeJiAmount = res.data.data[0].clear_out_amount;
        }
        if(this.formData.cashBoxType==1){ //硬币回收
          let res=await getClearOutAmount(this.formData);
          this.handOutHeJiAmount = res.data.data[0].clear_out_amount;
        }
        if(this.formData.cashBoxType==2){ //硬币找零
          this.handOutHeJiAmount = 0;
        }
        if(this.formData.cashBoxType==3){ //纸币回收
          let res=await getClearOutAmount(this.formData);
          this.handOutHeJiAmount = res.data.data[0].clear_out_paper_amount;
        }
        if(this.formData.cashBoxType==4){ //纸币找零
          this.handOutHeJiAmount = 0;
        }
        if(this.formData.cashBoxType==5){ //废钞箱
          this.handOutHeJiAmount = 0;
        }
      },
      closed(){
        this.dialogVisible=false;
        this.$parent.isShowMergeInfo=false;
      }
    },
    mounted() {

    }
  }
</script>
<style lang="scss">
  .huLianWangShouPiaojiDataInput {
    height: 100%;

    &__tree {
      padding-top: 3px;
      padding-right: 20px;
    }

    &__main {
      .el-card__body {
        padding-top: 0;
      }
    }
    .avue-form__group{

    }
  }
</style>
